<template>
	<el-dialog v-model="visible" :title="'选择班次'" :close-on-click-modal="false">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
			<el-form-item label="主键" prop="id" v-if="false">
				<el-input v-model="dataForm.id" placeholder="主键"></el-input>
			</el-form-item>

			<el-form-item label="休息日打卡" prop="jobSumTime">
				<el-input v-model="dataForm.jobSumTime" placeholder="休息日打卡"></el-input>
			</el-form-item>
		</el-form>

		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button @click="visible = false">清空排版</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>

	
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useShiftApi, useShiftSubmitApi } from '@/api/nb/attendance/shift'

const emit = defineEmits(['refreshDataList','openFind'])

const visible = ref(false)
const dataFormRef = ref()

const openFind=()=>{
	emit('openFind')
}

const dataForm = reactive({
	id: '',

	type: '1',
	name: '',
	jobSumTime: '',
	startTime: '',
	endTime: '',
	wealJson: '',
	isRest: 1,
	isClock: 1,
	isWeal: 1,
	isLate: 1,
	lateJson: '',
	value: ''
})
const resetData = () => {
	dataForm.lateJson = ''
	dataForm.startTime = ''
	dataForm.endTime = ''
	dataForm.wealJson = ''
}

const init = (id?: number) => {
	visible.value = true
	dataForm.id = ''

	// 重置表单数据
	if (dataFormRef.value) {
		resetData()
		dataFormRef.value.resetFields()
	}

	if (id) {
		getShift(id)
	}
}

const getShift = (id: number) => {
	useShiftApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useShiftSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>
